package com.fmdrama.db;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.fmdrama.bean.Video;
import com.fmdrama.util.JdbcUtil;

public class VideoDao {
	
	/**
	 * 批量新增
	 * @param videos
	 * @throws SQLException
	 */
	public int[] insert(List<Video> videos) throws SQLException{
		int len = videos.size();
		Object[][] params = new Object[len][7];
		for(int i=0; i<len; i++){
			Video video = videos.get(i);
			params[i][0] = video.getId();
			params[i][1] = video.getTitle();
			params[i][2] = video.getYouku();
			params[i][3] = video.getTudou();
			params[i][4] = video.getSohu();
			params[i][5] = video.getProfileId();
			params[i][6] = video.getFtime();
		}

		String sql = "INSERT INTO t_video(id, title, youku, tudou, sohu, profileId, ftime) VALUES(?,?,?,?,?,?,?)";
		QueryRunner query = new QueryRunner(true);
		return query.batch(JdbcUtil.getConnection(), sql, params);
	}
	
	/**
	 * 根据profileId 查询
	 * @param profileId
	 * @return
	 * @throws SQLException
	 */
	public List<Video> queryByProfileId(long profileId) throws SQLException{
		String sql = "SELECT * FROM t_video WHERE profileId=?";
		QueryRunner query = new QueryRunner(true);
		List<Video> videos = query.query(JdbcUtil.getConnection(), sql, new BeanListHandler<Video>(Video.class), profileId);
		return videos;
	}
	
}
